package com.jsmile.cloud.authc.help.repo;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsmile.cloud.authc.api.req.sys.ReqSysHelpQuery;
import com.jsmile.cloud.authc.help.dao.SysHelpDao;
import com.jsmile.cloud.authc.help.model.SysHelp;
import com.jsmile.mall.api.kit.DateKit;
import com.jsmile.mall.api.kit.StrKit;

import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2020年10月19日 03:27:57
 */
@Service
@Slf4j
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class SysHelpSearchRepository extends ServiceImpl<SysHelpDao, SysHelp> {

    public IPage<SysHelp> pageQuery(ReqSysHelpQuery reqSysHelpQuery) {
        return this.baseMapper.selectPage(reqSysHelpQuery.getPage(), getQueryWrapper(reqSysHelpQuery));
    }

    public List<SysHelp> list(ReqSysHelpQuery reqSysHelpQuery) {
        return this.baseMapper.selectList(getQueryWrapper(reqSysHelpQuery));
    }

    private Wrapper getQueryWrapper(ReqSysHelpQuery reqSysHelpQuery) {
        return new QueryWrapper<SysHelp>().lambda().eq(null != reqSysHelpQuery.getCurTenantId(), SysHelp::getTenantId, reqSysHelpQuery.getCurTenantId())
            .like(StrKit.notBlank(reqSysHelpQuery.getHelpTitle()), SysHelp::getHelpTitle, reqSysHelpQuery.getHelpTitle())
            .like(StrKit.notBlank(reqSysHelpQuery.getHelpContent()), SysHelp::getHelpContent, reqSysHelpQuery.getHelpContent())
            .and(StrKit.notBlank(reqSysHelpQuery.getKeyword()),
                wrapper -> wrapper.like(SysHelp::getHelpTitle, reqSysHelpQuery.getKeyword()).or().like(SysHelp::getHelpCode, reqSysHelpQuery.getKeyword()).or()
                    .like(SysHelp::getHelpContent, reqSysHelpQuery.getKeyword()))
            .eq(null != reqSysHelpQuery.getHelpType(), SysHelp::getHelpType, reqSysHelpQuery.getHelpType())
            .eq(null != reqSysHelpQuery.getHelpCode(), SysHelp::getHelpCode, reqSysHelpQuery.getHelpCode())
            .eq(null != reqSysHelpQuery.getEnable(), SysHelp::getEnable, reqSysHelpQuery.getEnable())
            .ge(null != reqSysHelpQuery.getStartDate(), SysHelp::getCreateTime, DateKit.beginOfDay(reqSysHelpQuery.getStartDate()))
            .le(null != reqSysHelpQuery.getEndDate(), SysHelp::getCreateTime, DateKit.endOfDay(reqSysHelpQuery.getEndDate())).orderByDesc(SysHelp::getCreateTime);// sort,createTime倒序
    }
}
